/**
 * 文件名: MessageSendUtils.java 版 权: Copyright © 2013 - 2018 CFNE, Inc. All Rights
 * Reserved 描 述: &lt;描述&gt; 修改人: WeiQiang.Fan 修改时间: 2018/10/18 跟踪单号:
 * &lt;跟踪单号&gt; 修改单号: &lt;修改单号&gt; 修改内容: &lt;修改内容&gt;
 */
package com.cnswhy.cloud.server.iot.utils;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.security.SecureRandom;

/**
 * 功能描述 :
 * @author vansee
 * @version [版本号,  2018/10/18]
 * @see [相关类/方法]
 * @since [产品/模板版本号]
 */
public class MessageSendUtils
{
    
    public static String sendMMS(String phoneNo, String content) throws Exception
    {
        String content1 = java.net.URLEncoder.encode(content + "【人人拼】", "UTF-8");
        String postContent = "userid=125501&account=13000000007&password="+ MD5.md5s("a123456")+"&content="
                + content1 + "&mobile=" + phoneNo+"&action=send";
        String httpGetConnect = httpPostRequest(new URL("https://dx.ipyy.net/sms.aspx"), postContent);
        System.out.println(httpGetConnect);
        return httpGetConnect;
    }
    
    public static void main(String[] args) throws Exception
    {
        sendMMS("18948773137", "您的验证码为：6548，10分钟内有效。验证码提供给他人可能导致账号被盗，请勿泄露，谨防被骗。");
//        System.out.println(UUID.randomUUID().toString().replace("-", ""));
    }
    
    public static final String generateSecureCode(int length)
    {
        StringBuilder builder = new StringBuilder();
        char[] chars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
        SecureRandom random = new SecureRandom();
        for (int i = 0; i < length; i++)
        {
            int j = random.nextInt(10);
            builder.append(chars[j]);
        }
        return builder.toString();
    }
    
    public static String httpPostRequest(URL url, String postContent) throws Exception
    {
        OutputStream outputstream = null;
        BufferedReader in = null;
        try
        {
            URLConnection httpurlconnection = url.openConnection();
            httpurlconnection.setConnectTimeout(10 * 1000);
            httpurlconnection.setDoOutput(true);
            httpurlconnection.setUseCaches(false);
            OutputStreamWriter out = new OutputStreamWriter(httpurlconnection.getOutputStream(), "UTF-8");
            out.write(postContent);
            out.flush();
            
            StringBuffer result = new StringBuffer();
            in = new BufferedReader(new InputStreamReader(httpurlconnection.getInputStream(), "UTF-8"));
            String line;
            while ((line = in.readLine()) != null)
            {
                result.append(line);
            }
            return result.toString();
        }
        catch (Exception ex)
        {
            throw new Exception("post请求异常：" + ex.getMessage());
        }
        finally
        {
            if (outputstream != null)
            {
                try
                {
                    outputstream.close();
                }
                catch (IOException e)
                {
                    outputstream = null;
                }
            }
            if (in != null)
            {
                try
                {
                    in.close();
                }
                catch (IOException e)
                {
                    in = null;
                }
            }
        }
    }
    
   
}